<script setup lang="ts">
//
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import { getHomeBanner, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { XZJGuessInstance } from '@/types/component'

const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
const isLoading = ref<Boolean>(false)
const getHomeBannerData = async () => {
  const res = await getHomeBanner()
  bannerList.value = res.result
}
const getCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}
const getHotList = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
const guessRef = ref<XZJGuessInstance>()
//滚动触底
const onScrollToLower = () => {
  guessRef.value?.getMore()
}
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryData(), getHotList()])
  isLoading.value = false
})
const isTriggered = ref(false)
const onrefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  // 加载数据
  await Promise.all([
    getHotList(),
    getCategoryData(),
    getHomeBannerData(),
    guessRef.value?.resetData(),
  ]) // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <CustomNavbar />
  <scroll-view
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="onrefresherrefresh"
    scroll-y
    @scrolltolower="onScrollToLower"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <XZJSwiper :list="bannerList" />
      <CategoryPanel :list="categoryList" />
      <HotPanel :list="hotList" />
      <XZJGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
